<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\OrderTicket;
use app\common\model\User;
use think\Log;

/**
 * adapay接口
 */
class Adapay extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 异步通知
     *
     */
    public function asyncNotify()
    {
        # 回调接收（接收回调要用post方式）
        $post_data      = json_decode($_POST["data"], 1);
        $payment_id = $_POST['id'];
        Log::record("adapay异步通知参数");
        Log::record($post_data);
        Log::record('$payment_id:'.$payment_id);
        switch ($post_data["status"]) {
            case "succeeded":
                # 回调成功
                $aData = [
                    # 官方返回数据id
                    "request_id"   => $post_data["id"],
                    # 时间
                    "created_time" => $post_data["created_time"],
                    # 订单号
                    "order_no"     => $post_data["order_no"],
                    # app_id
                    "app_id"       => $post_data["app_id"],
                    # 支付渠道
                    "pay_channel"  => $post_data["pay_channel"],
                    # 付款金额
                    "pay_amt"      => $post_data["pay_amt"],
                    # 交易订单号
                    "id"           => $post_data["id"],
                    # 状态
                    "status"       => $post_data["status"],
                    # openid
                    "sub_open_id"  => $post_data["expend"]["sub_open_id"]
                ];
                $order_type = $this->request->param('order_type');
                Log::record('$order_type:'.$order_type);
                $out_trade_no = $post_data["order_no"];
                if($order_type == 'buy'){
                    Log::record("购买订单");
                    $order = \app\admin\model\Order::where(['sn'=>$out_trade_no,'status'=>0])->find();
                    if (!$order){
                        return json(['code' => 'SUCCESS', 'message' => '成功'], 200, ['Content-Type' => 'application/json']);
                    }
                    $order -> status = 1;
                    $order -> pay_type = 'adapay_wx_pub';
                    $order -> payment_id = $post_data["id"];
                    $order -> pay_time = time();

                    $order -> save();
                    OrderTicket::where(['status'=>0,'order_id'=>$order->id])->update(['status'=>1]);
                }else if($order_type == 'recharge'){
                    Log::record("充值订单");
                    Log::record($out_trade_no);
                    $order = \app\admin\model\recharge\Order::where(['order_no' => $out_trade_no,'status'=>10])->find();
                    Log::record($order);
                    if (!$order){
                        return json(['code' => 'SUCCESS', 'message' => '成功'], 200, ['Content-Type' => 'application/json']);
                    }
                    $order -> status = 20;
                    $order -> pay_type = 'adapay_wx_pub';
                    $order -> pay_time = time();
                    $order -> save();
                    Log::record("以上没问题");

                    User::money($order->money,$order->user_id,'订单：'.$order->order_no.'充值');
                    //不再有赠送余额钱包，直接赠送到钱包
                    User::money($order->gift_money,$order->user_id,'订单：'.$order->order_no.'充值赠送余额');
                    User::giveMoney($order->gift_money,$order->user_id); //,'订单：'.$order->order_no.'充值赠送余额'
                    User::get($order->user_id) -> setInc('total_recharge', $order->money);

                    $param['user_id'] = $order->user_id;
                    $WechatApi = new \addons\epay\controller\Api();
                    $WechatApi -> checkUserLevel($param);
                }
                unset($aData);
                break;
            default:
                # 回调失败
                return json(['code' => 'FAIL', 'message' => '失败'], 200, ['Content-Type' => 'application/json']);
        }

        return json(['code' => 'SUCCESS', 'message' => '成功'], 200, ['Content-Type' => 'application/json']);
    }


}
